#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Game02;
using UnityEngine;
public class Editor_LightMap: MonoBehaviour {
    [UnityEditor.MenuItem("GameObject/go工具/设置烘焙贴图")]
    public static void ssss() {
        var objects = UnityEditor.Selection.objects;
        if(objects == null) {
            return;
        }
        var root = objects[0] as GameObject;
        if(root == null) {
            return;
        }
        var instance = root.GetComponent<LightMapLoader>();
        if(instance == null) {
            return;
        }
        var rendererInfos = new List<LightmapRendererInfo>();
        var lightmapsColor = new List<Texture2D>();
        var lightmapsDir = new List<Texture2D>();

        GenerateLightmapInfo(root, rendererInfos, lightmapsColor, lightmapsDir);
        instance.lightMapInfoArr = rendererInfos.ToArray();
        instance.lightmapColorArr = lightmapsColor.ToArray();
        instance.lightmapDirArr = lightmapsDir.ToArray();
    }

    static void GenerateLightmapInfo(GameObject root, List<LightmapRendererInfo> rendererInfos, List<Texture2D> lightmapsColor, List<Texture2D> lightmapsDir) {
        var renderers = root.GetComponentsInChildren<MeshRenderer>();
        foreach(MeshRenderer renderer in renderers) {
            if(renderer.lightmapIndex != -1) {
                var info = new LightmapRendererInfo();
                info.renderer = renderer;
                if(renderer.lightmapScaleOffset != Vector4.zero) {
                    info.lightmapOffsetScale = renderer.lightmapScaleOffset;
                    Texture2D lightmapColor = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor;
                    Texture2D lightmapDir = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapDir;
                    info.lightmapIndex = lightmapsColor.IndexOf(lightmapColor);
                    if(info.lightmapIndex == -1) {
                        info.lightmapIndex = lightmapsColor.Count;
                        lightmapsColor.Add(lightmapColor);
                        lightmapsDir.Add(lightmapDir);
                    }
                    rendererInfos.Add(info);
                }
            }
        }

        var Terrainrenderers = root.GetComponentsInChildren<Terrain>();
        foreach(var terrain in Terrainrenderers) {
            if(terrain.lightmapIndex != -1) {
                var info = new LightmapRendererInfo();
                info.terrain = terrain;
                if(terrain.lightmapScaleOffset != Vector4.zero) {
                    info.lightmapOffsetScale = terrain.lightmapScaleOffset;
                    Texture2D lightmapColor = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapColor;
                    Texture2D lightmapDir = LightmapSettings.lightmaps[terrain.lightmapIndex].lightmapDir;
                    info.lightmapIndex = lightmapsColor.IndexOf(lightmapColor);
                    if(info.lightmapIndex == -1) {
                        info.lightmapIndex = lightmapsColor.Count;
                        lightmapsColor.Add(lightmapColor);
                        lightmapsDir.Add(lightmapDir);
                    }
                    rendererInfos.Add(info);
                }
            }
        }
    }
}
#endif
